package com.hhl.lamda.comparator;

/**
 * @author HHL
 * @description
 * @date 2018/11/16 - 17:00
 */
import com.hhl.lamda.entity.Developer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test1Sorting {

    public static void main(String[] args) {

        List<Developer> listDevs = getDevelopers();

        System.out.println("Before Sort");
        for (Developer developer : listDevs) {
            System.out.println(developer);
        }




        /* 通过age排序 */
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println("通过age排序");
        for (Developer developer : listDevs) {
            System.out.println(developer);
        }






        /* 通过name排序 */
        Collections.sort(listDevs, new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        System.out.println("通过name排序");
        for (Developer developer : listDevs) {
            System.out.println(developer);
        }
        System.out.println();
        System.out.println();
        System.out.println();






        /* Lambda 表达式 */
        listDevs.sort(new Comparator<Developer>() {
            @Override
            public int compare(Developer o1, Developer o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        System.out.println("Lambda 表达式-通过name排序");
        for (Developer developer : listDevs) {
            System.out.println(developer);
        }
        listDevs.forEach((developer) -> System.out.println("Lambda 表达式-通过name排序" + developer));





        /* Lambda 表达式 逆序排列 */
        Comparator<Developer> nameComparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
        listDevs.sort(nameComparator.reversed());
        listDevs.forEach(developer ->  System.out.println("Lambda 表达式 逆序排列-通过name排序" + developer));


        System.out.println();
        System.out.println();
        System.out.println();

        /* Lambda 表达式 逆序排列 */
        Comparator<Developer> ageComparator = (o1, o2) -> o1.getAge() - o2.getAge();
        listDevs.sort(ageComparator.reversed());
        listDevs.forEach(developer ->   System.out.println("Lambda 表达式 逆序排列-通过age排序" + developer));


    }

    private static List<Developer> getDevelopers() {

        List<Developer> result = new ArrayList<Developer>();

        result.add(new Developer("mkyong", 70000, 33));
        result.add(new Developer("alvin", 80000, 20));
        result.add(new Developer("jason", 100000, 10));
        result.add(new Developer("iris", 170000, 55));

        return result;

    }

}

